import {putActricle,updateActricle,GetDetailTitle,deleOnePicture,GetAllPicId,addOnePicture,getAllPicture,updateOnePicture,GetIndexAllACtircle,DeleteActricle} from '@/api/titleOrPicture'
const state = {
    actircleinfo: [], //文章内容
    AllImageInfo:[],//图片管理
    allIdinfo:[]
}
const actions = {
    //拿到所有文章或分类查看
    async GetindexAllInfo(context, aDataInfo) {
		const {
			category_id,
			current,
			orderByTime,
			size,
			title
		} = aDataInfo
		let res = await GetIndexAllACtircle({
			category_id,
			current,
			orderByTime,
			size,
			title
		})
		if (res.data.code === 0) {
			context.commit('GETALLACTICLE', res.data.result.list)
			return 'ok'
		}
	},
    //发布文章
    async PutAcricle(context,data){
        // const {articleBody,authorName,backgrundAdress,imgFileAdress,littleCategoryId,summary,title} = data
        let res = await putActricle(data)
        if(res.data.code == 0){
            return 'ok'
        }else{
            return 'fail'
        }
    },

    //修改文章
     async UpdateTic(context,data){
         console.log(data)
        let res = await updateActricle({...data})
        console.log(res)
        if(res.data.code == 0){
            return 'ok'
        }
        
     },
     //拿到具体文章
     async getDetailTitle(context,id){
        let res = await GetDetailTitle(id)
        if(res.data.code == 0){
            return res.data.result.articleBody
        }
        //articleBody
        // console.log(res)
     },
     //删除文章
     async deleteActricle(context,id){
        let res = await DeleteActricle(id)
        if(res.data.code == 0){
            return 'ok'
        }
     },
     //删除图片
     async delOnePic(context,data){
        let res = await deleOnePicture(data)
        if(res.data.code == 0){
            return 'ok'
        }
        // console.log(res)
     },
     //增加一张图片
     async AddOnePic(context,data){
        let res = await addOnePicture(data)
        if(res.data.code == 0){
            return 'ok'
        }
        // console.log(res)
     },
     //得到所有图片
     async GetAllPic(context){
        let res = await  getAllPicture()
        if(res.data.code === 0){
            context.commit('SETALLIMAGE',res.data.result)
            return 'ok';
        }
     },
     //更新一张图片
     async UpdateOnePic(context,data){
        let res = await updateOnePicture(data)
        if(res.data.code === 0){
            return 'ok'
        }
     },
     //拿到所有图片
     async getAllPicId(context){
        let res = await GetAllPicId()
        // console.log(res)
        if(res.data.code == 0){
            context.commit('SETALLIMGID',res.data.result)
            return 'ok'
        }
     }

}
const mutations = {
    //设置文章
	GETALLACTICLE(state, info) {
		state.actircleinfo = info
	},
    //设置图片
    SETALLIMAGE(state,data){
        state.AllImageInfo = data
    },
    //设置所有文章id
    SETALLIMGID(state,data){
        state.allIdinfo = data
    }
}
const getters = {
    ImgTitleId(state){
        const data = state.allIdinfo
        console.log(data)
        let newdata = []
        data.forEach((item) => {
            const {id,title} = item
            const newobj = {id,title}
            newdata.push(newobj)
        });
        return newdata
    }
}
export default {
    namespaced: true,
    state,
    actions,
    mutations,
    getters
}